package com.luoqiz.demo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;

import com.luoqiz.demo.abstractDemo.LaoluoRoom;
import com.luoqiz.demo.adapter.Button;
import com.luoqiz.demo.adapter.MouseAdapter;
import com.luoqiz.demo.constractDemo.Dog;
import com.luoqiz.demo.model.Person;

/**
 * ioDemo请到ioDemo包中测试
 * @author luoqiz
 *
 */
public class DemoTest {

	@Test
	public void abstractTest() {
		System.out.println("*****************************************");
		System.out.println("测试抽象类的静态代码块和构造函数");
		LaoluoRoom l = new LaoluoRoom();
		l.sleepRoom();
		l.restRoom();
		System.out.println();
	}

	@Test
	public void ConstruactTest() {
		System.out.println("*****************************************");
		System.out.println("测试静态代码块、构造代码块、构造函数的执行顺序，以及静态属性");
		// 局部代码块
		{
			System.out.println("局部代码块--------");
		}
		//匿名类
		new Dog("1");
		Dog dog2 = new Dog("2");

		System.out.println("Dog blood:" + Dog.getBlood());
		System.out.println("Dog height:" + Dog.height);
		dog2.setBlood("erha");
		dog2.height = 20;
		System.out.println("dog2 blood:" + dog2.getBlood());
		System.out.println("dog2 height:" + dog2.height);
		System.out.println("dog name:" + dog2.getName());
		System.out.println();

	}

	@Test
	public void StringJoinTest() {
		System.out.println("*****************************************");
		System.out.println("String连接时内存占用测试");
		String s1 = "阿百川"; // 占用一块内存
		String s2 = "123"; // 占用一块内存
		String s3 = s1 + s2; // 新占用一块内存，不会再原来的s1上连接。
		String s4 = "阿百川123"; // 新占用一块内存,不和s3有联系
		System.out.println(s3 == s4); // 结果false。在字符串中s3和s4所在内存不同
		System.out.println(s3.equals(s4)); //true s3和s4所在内存不同，值相同
		int i = 5;
		int j = 5;
		System.out.println(i == j); // 结果true,判断值
		System.out.println();

	}

	@Test
	public void bubbleSortTest() {
		System.out.println("*****************************************");
		System.out.println("冒泡排序");
			// 冒泡排序
			int[] arr = { 5, 3, 4, 2, 1, 9, 6 };
			int n = arr.length - 1;
			for (int i = n; i > 0; i--) {
				for (int j = 0; j < i; j++) {
					if (arr[j] > arr[j + 1]) {
						int temp = arr[j + 1];
						arr[j + 1] = arr[j];
						arr[j] = temp;
					}
				}
			}
			for (int i : arr) {
				System.out.println(i);
			}
		System.out.println();

	}

	@Test
	public void binarySearchTest() {
		System.out.println("*****************************************");
		System.out.println("二分法查找");
		int[] arr = { 2, 3, 5, 7, 8, 9, 13, 15, 17, 18, 19, 25, 28, 37, 39, 46, 49, 57, 59, 60, 69, 79, 88, 99 };
		int minIndex = 0;
		int maxIndex = arr.length - 1;
		int search = 88;
		boolean isFind = false;
		while (minIndex <= maxIndex) {
			int mid = (minIndex + maxIndex) / 2;
			if (arr[mid] == search) {
				System.out.println("index=" + mid);
				System.out.println("arr[" + mid + "]=" + arr[mid]);
				isFind = true;
				break;
			} else if (arr[mid] < search) {
				minIndex = mid + 1;
			} else if (arr[mid] > search) {
				maxIndex = mid - 1;
			}
		}
		if (!isFind) {
			System.out.println("未找到");
		}
		System.out.println();

	}

	@Test
	public void adapoter1Test() {
		System.out.println("*****************************************");
		System.out.println("适配器模式测试");
		Button b1 = new Button();
		b1.addListener(new MouseAdapter() {
			public void onClick() {
				System.out.println("b1单击");
			}
		});
		b1.onClick();
		b1.onDbClick();
		System.out.println();

	}

	@Test
	public void arraycopyTest() {
		System.out.println("*****************************************");
		System.out.println("浅拷贝测试");
		int[] src={1,2,3,4};
		int[] dest=new int[5];
		System.arraycopy(src, 1, dest, 2, 2);
		src[1]=99;
		for (int string : src) {
			System.out.print(string+"---");
		}
		System.out.println();
		for (int string : dest) {
			System.out.print(string+"---");
		}
		System.out.println();
		Person[] src1={new Person("1"),new Person("2"),new Person("3"),new Person("4")};
		Person[] dest1=new Person[5];
		System.arraycopy(src1, 0, dest1, 0, 2);
		src1[1].setName("99");;
		for (Person string : src1) {
			System.out.print(string.getName()+"---");
		}
		System.out.println();
		for (Person string : dest1) {
			System.out.print(string==null?"null---":string.getName()+"---");
		}
		System.out.println();

	}

	@Test
	public void StringToHex() {
		System.out.println("*****************************************");
		System.out.println("字符串转十六进制");
		char[] c={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
		String s = "罗"; // 7f57
		int tenJin = (int) s.charAt(0);
		//十进制转二进制
		StringBuilder sb=new StringBuilder();
		for (int i = 31; i >= 0; i = i - 1) {
			sb.append(tenJin >> i & 0x1);
		}
		//二进制转16进制
		StringBuilder sl=new StringBuilder();
		for(int i=0,n=sb.toString().length()-1;i<n;i=i+4){
			String temp=sb.substring(i, i+4);
			sl.append(c[test2(temp)]);
		}
		System.out.println(sl);
		System.out.println();

	}
	public static int test2(String s){
		int sum=0;
		for(int i=s.length()-1;i>=0;i--){
			int temp=Integer.parseInt(String.valueOf(s.charAt(i)))<<(3-i);
			sum=sum+temp;
		}
		return sum;
	}
	@Test
	public void getListWayTest() {
		System.out.println("*****************************************");
		System.out.println("获取list内容方式");
		List<String> a = new ArrayList<>();
		a.add("123");
		a.add("444");
		a.add("5");
		a.add("6");
		// 遍历获取
		System.out.println("for循环获取");
		for (int i = 0, n = a.size(); i < n; i++) {
			System.out.print(a.get(i).trim() + "---");
		}
		System.out.println();
		System.out.println("增强for循环获取");
		for (String s : a) {
			System.out.print(s + "---");
		}
		System.out.println();
		// 迭代器获取
		System.out.println("迭代器获取");
		Iterator<String> it = a.iterator();
		while (it.hasNext()) {
			System.out.print(it.next().trim() + "---");
		}
		System.out.println();

	}
	@Test
	public void adapoter8() {
		System.out.println("*****************************************");
		System.out.println("");

		System.out.println();

	}

}
